package com.sztx.pay.center.rpc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.JFUtils;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskNopwdPayMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.RiskNopwdPayDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserInfoDO;
import com.sztx.pay.center.rpc.api.domain.NoPwdPayCheckRequest;
import com.sztx.pay.center.rpc.api.domain.NoPwdPayGetResponse;
import com.sztx.pay.center.rpc.api.domain.NoPwdPaySetRequest;
import com.sztx.pay.center.rpc.api.service.NoPwdPayRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.core.service.impl.BaseServiceImpl;
@Service
public class NoPwdPayRpcServiceImpl extends BaseServiceImpl implements NoPwdPayRpcService{
	@Autowired
	RiskNopwdPayMysqlDAO riskNopwdPayMysqlDAO;
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	ActService actService;
	
	@Override
	public void noPwdPaySet(NoPwdPaySetRequest noPwdPaySetRequest) {
		ValidateUtil.isNotBlank(noPwdPaySetRequest.getPayerUserId(), "payerUserId不能为空");
		ValidateUtil.le(noPwdPaySetRequest.getTradeType().intValue(), 8, "tradeType参数错误");
		ValidateUtil.ge(noPwdPaySetRequest.getMaxAmount().intValue(), 1, "设置免密支付最大金额不能小于1");
		ValidateUtil.isBetween(noPwdPaySetRequest.getStatus(), 0, 1, "status参数错误");
		ValidateUtil.isNotBlank(noPwdPaySetRequest.getChanneID(), "渠道ID不能为空");
		
		
		
		UserInfoDO userInfoDO = userInfoService.findUserInfo(noPwdPaySetRequest.getPayerUserId());
		if(userInfoDO == null){
			throw new BusinessException("帐号【"+noPwdPaySetRequest.getPayerUserId()+"】不存在");
		}
		Integer actType = noPwdPaySetRequest.getActType();
		if(actType == null){
			boolean isJFChannel = JFUtils.isJFChannel(noPwdPaySetRequest.getChanneID());
			if(isJFChannel){
				actType = UserActType.JF.getCode();
			}else{
				actType = UserActType.CASH.getCode();
			}
		}
		UserActDO userActDO = actService.findUserAct(noPwdPaySetRequest.getPayerUserId(), UserActType.valueOf(actType));
		if(userActDO == null){
			throw new BusinessException("帐号【"+noPwdPaySetRequest.getPayerUserId()+"】的账户类型为【"+actType+"】不存在");
		}
		
		RiskNopwdPayDO riskNopwdPayDO = riskNopwdPayMysqlDAO.
				findBypayer(noPwdPaySetRequest.getPayerUserId(), noPwdPaySetRequest.getTradeType().intValue(),noPwdPaySetRequest.getChanneID(),actType);
		
		
		if(riskNopwdPayDO == null){
			//插入数据
			RiskNopwdPayDO riskNopwdPayDO2 = new RiskNopwdPayDO();
			riskNopwdPayDO2.setChannelId(noPwdPaySetRequest.getChanneID());
			riskNopwdPayDO2.setActType(actType);
			riskNopwdPayDO2.setAmount(new Long("0"));
			
			riskNopwdPayDO2.setMaxAmount(noPwdPaySetRequest.getMaxAmount());
			riskNopwdPayDO2.setPayerUserId(userActDO.getUserId());
			riskNopwdPayDO2.setStatus(noPwdPaySetRequest.getStatus());
			riskNopwdPayDO2.setTradeType(noPwdPaySetRequest.getTradeType());
			riskNopwdPayDO2.setRemark(noPwdPaySetRequest.getRemark());
			riskNopwdPayMysqlDAO.save(riskNopwdPayDO2);
		}else{
			//重置累计金额 
			riskNopwdPayDO.setAmount(new Long("0"));
			riskNopwdPayDO.setMaxAmount(noPwdPaySetRequest.getMaxAmount());
			riskNopwdPayDO.setStatus(noPwdPaySetRequest.getStatus());
			riskNopwdPayDO.setTradeType(noPwdPaySetRequest.getTradeType());
			riskNopwdPayDO.setRemark(noPwdPaySetRequest.getRemark());
			riskNopwdPayDO.setChannelId(noPwdPaySetRequest.getChanneID());
			riskNopwdPayDO.setActType(actType);
			riskNopwdPayMysqlDAO.update(riskNopwdPayDO);
		}
	}

	@Override
	@SlaveQueryAnnotation
	public boolean noPwdPayCheck(NoPwdPayCheckRequest noPwdPayCheckRequest) {
		ValidateUtil.isNotBlank(noPwdPayCheckRequest.getPayerUserId(), "payerUserId不能为空");
		ValidateUtil.le(noPwdPayCheckRequest.getTradeType().intValue(), 8, "tradeType参数错误");
		ValidateUtil.ge(noPwdPayCheckRequest.getAmount().intValue(), 1, "amount参数错误");
		ValidateUtil.isNotBlank(noPwdPayCheckRequest.getChanneID(), "渠道ID不能为空");
		
		
		Integer actType = noPwdPayCheckRequest.getActType();
		if(actType == null){
			boolean isJFChannel = JFUtils.isJFChannel(noPwdPayCheckRequest.getChanneID());
			if(isJFChannel){
				actType = UserActType.JF.getCode();
			}else{
				actType = UserActType.CASH.getCode();
			}
		}
		
		
		RiskNopwdPayDO riskNopwdPayDO = riskNopwdPayMysqlDAO.
				findBypayer(noPwdPayCheckRequest.getPayerUserId(), noPwdPayCheckRequest.getTradeType().intValue(),noPwdPayCheckRequest.getChanneID(),actType);
		if(riskNopwdPayDO == null || riskNopwdPayDO.getStatus() == 0){
			return false;
		}
		Long maxAmount = riskNopwdPayDO.getMaxAmount();
		Long accAmount = riskNopwdPayDO.getAmount();
		Long amount = noPwdPayCheckRequest.getAmount();
		if((amount + accAmount) <= maxAmount){
			return true;
		}
		return false;
	}

	@Override
	@SlaveQueryAnnotation
	public NoPwdPayGetResponse noPwdPayGet(String payerUserId, Integer tradeType,String channelId,Integer actType) {
		ValidateUtil.isNotBlank(payerUserId, "payerUserId不能为空");
		ValidateUtil.le(tradeType, 8, "tradeType参数错误");
		NoPwdPayGetResponse response = new NoPwdPayGetResponse();
		RiskNopwdPayDO riskNopwdPayDO = riskNopwdPayMysqlDAO.findBypayer(payerUserId,tradeType,channelId,actType);
		if(riskNopwdPayDO != null){
			response.setCurrAmount(riskNopwdPayDO.getAmount());
			response.setMaxAmount(riskNopwdPayDO.getMaxAmount());
			response.setPayerUserId(riskNopwdPayDO.getPayerUserId());
			response.setStatus(riskNopwdPayDO.getStatus());
		}
		return response;
	}

}
